//****************************************//
//    mCore.cpp 18012012                  //
//    Riku Maikola rpm@ac-mapa.org        //
//****************************************//
#include "mLoad.h"
#include <wx/textfile.h>
#include <wx/tokenzr.h>



//***********************************//
//**                               **//
//***********************************//
bool mLoad::LoadImage(Image * IN,std::string filename)
{
    I = IN;
    I->Clear();

    if(filename.rfind(".dxf"))
    {
        DL_Dxf* dxf = new DL_Dxf();
        if (!dxf->in( filename,this))return false;

        delete dxf;
        return true;
    }
    /*else if(filename.Matches(_("*.stl")))
    {
        wxString str;
        wxTextFile file;
        if(!file.Open(filename))
            return false;

        mPoint pin[3];

        uint A=0;
        for(size_t a=0; a<file.GetLineCount(); a++)
        {
            str = file.GetLine(a) << _("\n");

            wxStringTokenizer tkz(str, wxT(" "),wxTOKEN_STRTOK);
            while ( tkz.HasMoreTokens() )
            {
                wxString token = tkz.GetNextToken();

                if( token.Matches(_("outer")))
                {
                    A=0;
                    continue;
                }

                if( token.Matches(_("endsolid")))break;

                if( token.Matches(_("facet")))continue;
                if( token.Matches(_("endfacet\n")))continue;
                if( token.Matches(_("normal")))continue;
                if( token.Matches(_("loop\n")))continue;

                if( token.Matches(_("vertex"))&& (tkz.CountTokens()==3))
                {

                    double value;
                    tkz.GetNextToken().ToDouble(&value);
                    double v1 = value*1000;
                    tkz.GetNextToken().ToDouble(&value);
                    double v2 = value*1000;
                    tkz.GetNextToken().ToDouble(&value);
                    double v3 = value*1000;

                    pin[A].SetValue(v1,v2,v3);

                    A++;
                    continue;
                }

                if( token.Matches(_("endloop\n")) )
                {
                    I->add( eT( pin[0],pin[1],pin[2] )  );
                    continue;
                }
            }
        }
        I->SetType(m_Mesh);
        //I->Update();
    }*/

    return false;
}



//***********************************//
//**                               **//
//***********************************//
bool mLoad::LoadImage(Image * IN,wxString filename)
{
    I = IN;
    I->Clear();

    if(filename.Matches(_("*.dxf")))
    {
        DL_Dxf* dxf = new DL_Dxf();
        if (!dxf->in( std::string(filename.mb_str()),this))return false;

        delete dxf;
        return true;
    }
    else if(filename.Matches(_("*.stl")))
    {
        wxString str;
        wxTextFile file;
        if(!file.Open(filename))
            return false;

        mPoint pin[3];

        uint A=0;
        for(size_t a=0; a<file.GetLineCount(); a++)
        {
            str = file.GetLine(a) << _("\n");

            wxStringTokenizer tkz(str, wxT(" "),wxTOKEN_STRTOK);
            while ( tkz.HasMoreTokens() )
            {
                wxString token = tkz.GetNextToken();

                if( token.Matches(_("outer")))
                {
                    A=0;
                    continue;
                }

                if( token.Matches(_("endsolid")))break;

                if( token.Matches(_("facet")))continue;
                if( token.Matches(_("endfacet\n")))continue;
                if( token.Matches(_("normal")))continue;
                if( token.Matches(_("loop\n")))continue;

                if( token.Matches(_("vertex"))&& (tkz.CountTokens()==3))
                {

                    double value;
                    tkz.GetNextToken().ToDouble(&value);
                    double v1 = value*1000;
                    tkz.GetNextToken().ToDouble(&value);
                    double v2 = value*1000;
                    tkz.GetNextToken().ToDouble(&value);
                    double v3 = value*1000;

                    pin[A].SetValue(v1,v2,v3);

                    A++;
                    continue;
                }

                if( token.Matches(_("endloop\n")) )
                {
                    I->add( eT( pin[0],pin[1],pin[2] )  );
                    continue;
                }
            }
        }
        I->SetType(m_Mesh);
        //I->Update();
    }

    return false;
}
//***********************************//
//**                               **//
//***********************************//
bool mLoad::SaveImage(wxString file)
{
    /*  DL_Dxf dxf;
      DL_Codes::version exportVersion = DL_Codes::AC1015;
      DL_WriterA* dw = dxf.out(file.mb_str(), exportVersion);
      if (dw==NULL)return false;

      dxf.writeHeader(*dw);

      dw->sectionEnd();
      dw->sectionTables();
      dxf.writeVPort(*dw);

      dw->tableLineTypes(25);
      dxf.writeLineType(*dw, DL_LineTypeData("BYBLOCK", 0));
      dxf.writeLineType(*dw, DL_LineTypeData("BYLAYER", 0));
      dxf.writeLineType(*dw, DL_LineTypeData("CONTINUOUS", 0));
      dxf.writeLineType(*dw, DL_LineTypeData("ACAD_ISO02W100", 0));
      dxf.writeLineType(*dw, DL_LineTypeData("ACAD_ISO03W100", 0));
      dxf.writeLineType(*dw, DL_LineTypeData("ACAD_ISO04W100", 0));
      dxf.writeLineType(*dw, DL_LineTypeData("ACAD_ISO05W100", 0));
      dxf.writeLineType(*dw, DL_LineTypeData("BORDER", 0));
      dxf.writeLineType(*dw, DL_LineTypeData("BORDER2", 0));
      dxf.writeLineType(*dw, DL_LineTypeData("BORDERX2", 0));
      dxf.writeLineType(*dw, DL_LineTypeData("CENTER", 0));
      dxf.writeLineType(*dw, DL_LineTypeData("CENTER2", 0));
      dxf.writeLineType(*dw, DL_LineTypeData("CENTERX2", 0));
      dxf.writeLineType(*dw, DL_LineTypeData("DASHDOT", 0));
      dxf.writeLineType(*dw, DL_LineTypeData("DASHDOT2", 0));
      dxf.writeLineType(*dw, DL_LineTypeData("DASHDOTX2", 0));
      dxf.writeLineType(*dw, DL_LineTypeData("DASHED", 0));
      dxf.writeLineType(*dw, DL_LineTypeData("DASHED2", 0));
      dxf.writeLineType(*dw, DL_LineTypeData("DASHEDX2", 0));
      dxf.writeLineType(*dw, DL_LineTypeData("DIVIDE", 0));
      dxf.writeLineType(*dw, DL_LineTypeData("DIVIDE2", 0));
      dxf.writeLineType(*dw, DL_LineTypeData("DIVIDEX2", 0));
      dxf.writeLineType(*dw, DL_LineTypeData("DOT", 0));
      dxf.writeLineType(*dw, DL_LineTypeData("DOT2", 0));
      dxf.writeLineType(*dw, DL_LineTypeData("DOTX2", 0));
      dw->tableEnd();

    */

//layers
    /*    dw->tableLayers(L.size());
        for(uint a=0; a<L.size(); a++)
        {
            dxf.writeLayer(*dw,DL_LayerData( std::string(L.Item(a).mb_str())  , 0),DL_Attributes(std::string(""),      // leave empty
                           DL_Codes::white,      // default color
                           1,                  // default width
                           "CONTINUOUS"));       // default line style
        */


    /*
        dw->tableEnd();

    //Writing Various Other Tables
        dxf.writeStyle(*dw);
        dxf.writeView(*dw);
        dxf.writeUcs(*dw);

        dw->tableAppid(1);
        dw->tableAppidEntry(0x12);
        dw->dxfString(2, "ACAD");
        dw->dxfInt(70, 0);
        dw->tableEnd();

    //Writing Dimension Styles
        dxf.writeDimStyle(*dw,10,1,1,1,10);

        dxf.writeBlockRecord(*dw);
        //dxf.writeBlockRecord(*dw, "myblock1");
        //dxf.writeBlockRecord(*dw, "myblock2");
        dw->tableEnd();
        dw->sectionEnd();
        dw->sectionBlocks();
        /*dxf.writeBlock(*dw,DL_BlockData("*Model_Space", 0, 0.0, 0.0, 0.0));
        dxf.writeEndBlock(*dw, "*Model_Space");
        dxf.writeBlock(*dw,DL_BlockData("*Paper_Space", 0, 0.0, 0.0, 0.0));
        dxf.writeEndBlock(*dw, "*Paper_Space");
        dxf.writeBlock(*dw,DL_BlockData("*Paper_Space0", 0, 0.0, 0.0, 0.0));
        dxf.writeEndBlock(*dw, "*Paper_Space0");
        dxf.writeBlock(*dw,DL_BlockData("myblock1", 0, 0.0, 0.0, 0.0));*/
// ...
// write block entities e.g. with dxf.writeLine(), ..
// ...
    //dxf.writeEndBlock(*dw, "myblock1");
    // dxf.writeBlock(*dw,DL_BlockData("myblock2", 0, 0.0, 0.0, 0.0));
// ...
// write block entities e.g. with dxf.writeLine(), ..
// ...
    //dxf.writeEndBlock(*dw, "myblock2");

    /* dw->sectionEnd();


     dw->sectionEntities();

    // write all your entities..
     for(uint a=0; a<ME.size(); a++)
     {
         //double x = gV(a,1).x;
         //if(layer.ME.at(a).type==0)dxf.writePoint(*dw,DL_PointData(10.0,45.0,0.0),DL_Attributes(std::string(LNames.Item(a).mb_str()), 256, -1, "BYLAYER"));
         if(ME.at(a)->T()==1)
         {
             dxf.writeLine(*dw,DL_LineData(
                               ME.at(a)->p1().x,    ME.at(a)->p1().y,  ME.at(a)->p1().z,
                               ME.at(a)->p2().x,    ME.at(a)->p2().y,  ME.at(a)->p2().z )
                           ,DL_Attributes(   std::string( "0"   )    , 256, -1, "BYLAYER"));
         }
         if(ME.at(a)->T()==3)
         {
             dxf.writeArc(*dw,DL_ArcData(
                              ME.at(a)->cp().x,    ME.at(a)->cp().y,  ME.at(a)->cp().z,
                              ME.at(a)->r,  ME.at(a)->a1*180/PI,    ME.at(a)->a2*180/PI )
                          ,DL_Attributes(std::string("0"), 256, -1, "BYLAYER"));
         }
         if(ME.at(a)->T()==4)
         {
             dxf.writeCircle(*dw,DL_CircleData(
                                 ME.at(a)->cp().x,    ME.at(a)->cp().y,  ME.at(a)->cp().z, ME.at(a)->r)
                             ,DL_Attributes(std::string("0"), 256, -1, "BYLAYER"));
         }
     }
     dw->sectionEnd();
    //object section
     dxf.writeObjects(*dw);
     dxf.writeObjectsEnd(*dw);


    //Ending and Closing the File
     dw->dxfEOF();
     dw->close();
     delete dw;
     return true;
    }

    */
};



//***********************************//
//**                               **//
//***********************************//
void mLoad::addBlock(const DL_BlockData& data)
{
    //printf("BLOCK %s X%f  Y%6.3f Z%6.3f\n", data.name.c_str() , data.bpx , data.bpy , data.bpz);
    //wxString L(data.name.c_str(), wxConvUTF8);
    //addLayer(L,data.flags,attributes.getColor());
}
//***********************************//
//**                               **//
//***********************************//
void mLoad::addLayer(const DL_LayerData& data)
{
    //wxString name(data.name.c_str(), wxConvUTF8);
    //L.Add(name);
}

//*************************************//
//*****                              **//
//*************************************//
void mLoad::addPoint(const DL_PointData& data)
{
    wxString L(attributes.getLayer().c_str(), wxConvUTF8);
    //addPoint(data.x,data.y,L);
}
//*******************


void mLoad::addArc(const DL_ArcData& data)
{
    wxString L(attributes.getLayer().c_str(), wxConvUTF8);
    I->add( eA(mP(data.cx,data.cy),data.angle1*DegToRad,data.angle2*DegToRad,data.radius,L) );
};

void mLoad::addCircle(const DL_CircleData& data)
{
    wxString L(attributes.getLayer().c_str(), wxConvUTF8);
    I->add(eC( mP(data.cx,data.cy,data.cz),data.radius,L));
}


void mLoad::addLine(const DL_LineData& data)
{
    wxString L(attributes.getLayer().c_str(), wxConvUTF8);
    //printf("  Attributes: Layer: %s, ", attributes.getLayer().c_str());
    //printf("line %d\n",attributes.getColor());
    I->add( eL( mP(data.x1,data.y1,data.z1) , mP(data.x2,data.y2,data.z2),L));
};
